#coding=utf-8
from selenium import webdriver
import time
#random 随机数
import random
from PIL import Image
from ShowapiRequest import ShowapiRequest
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("http://heh.msn360.com/home/login/index.html")
time.sleep(5)
# EC.title_contains("登陆")
#判断页面中是否存在id为phone的元素，有的话执行文字输入，否则不执行
# locator = (By.ID,"phone")
# WebDriverWait(driver,1).until(EC.visibility_of_element_located(locator))
#截取网页存取图片
driver.save_screenshot("D:/seleniumpython/hyh-msn.png")
#输入账号
driver.find_element_by_id("phone").send_keys("13524559846")
#输入密码
driver.find_element_by_id("password").send_keys("123456")
#截取验证码，获取验证码图片，存储图片
code_element = driver.find_element_by_xpath("//*[@id='Imagecode_pic']")
left = code_element.location['x']
top = code_element.location['y']
right = code_element.size['width']+left
height = code_element.size['height']+top
im = Image.open('D:/seleniumpython/hyh-msn.png')
img = im.crop((left,top,right,height))
img.save('D:/seleniumpython/hyh-msn1.png')
#图片验证码识别
r = ShowapiRequest("http://route.showapi.com/184-4","62626","d61950be50dc4dbd9969f741b8e730f5" )
r.addBodyPara("typeId", "34")
r.addBodyPara("convert_to_jpg", "0")
r.addBodyPara("needMorePrecise", "0")
r.addFilePara("image", r"D:/seleniumpython/hyh-msn1.png") #文件上传时设置
res = r.post()
text = res.json()['showapi_res_body']['Result']
# print(text) # 返回信息
#输入验证码
driver.find_element_by_xpath('//*[@id="imageCode"]').send_keys(text)
#点击登陆
driver.find_element_by_class_name("submit").click()
#关闭页面
driver.close()




